package com.ayh.campusfun.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;


/**
 * @author ayh
 * @create 2021/3/18 0018 15:07
 * api返回数据封装
 */

@Data
public class APIResult {
    /** 成功消息  */
    protected static final String SUCCESS_MESSAGE = "操作成功！";
    /** 错误消息 */
    protected static final String ERROR_MESSAGE = "操作失败！";


    private String code;
    private String message;
    private Object data = new HashMap<String, Object>();

    public APIResult() {}
    public APIResult(Object data) {
        this.code = "1";
        if(data!=null){
            this.data = data;
        }
        this.message = SUCCESS_MESSAGE;
    }
    public APIResult(String code, String message) {
        this.code = code;
        this.message = message;
    }
    public APIResult(String code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static SerializeConfig config = new SerializeConfig();
    //对象为null返回{}
    //枚举类型处理
    public static SerializerFeature[] reatures = new SerializerFeature[] {
            SerializerFeature.WriteMapNullValue,//为null也返回
            SerializerFeature.WriteNullStringAsEmpty,//字符串为null，返回""
            SerializerFeature.WriteNullListAsEmpty,//数组/列表为null，返回[]
            SerializerFeature.WriteNullNumberAsZero,//数值类型（Integer,Long）为null，返回0
    };
    static {
        config.put(Date.class, new SimpleDateFormatSerializer());//日期类型处理，为null返回""
        config.put(BigDecimal.class, new BigDecimalSerializer());//BigDecimal类型处理，为null返回"0"
        config.setAsmEnable(false);
    }
    public static String error() {
        return JSON.toJSONString(new APIResult("0", ERROR_MESSAGE.toString()), config, reatures);
    }
    public static String error(String message) {
        return JSON.toJSONString(new APIResult("0",message), config, reatures);
    }
    public static String error(String code, String message) {
        return JSON.toJSONString(new APIResult(code,message), config, reatures);
    }
    public static String success() {
        return JSON.toJSONString(new APIResult("1", SUCCESS_MESSAGE.toString()), config, reatures);
    }
    public static String success(String code, String message){
        return JSON.toJSONString(new APIResult(code, message), config, reatures);
    }
    public static String success(String message) {
        return JSON.toJSONString(new APIResult("1", message), config, reatures);
    }
    public static String successData(Object data) {
        return JSON.toJSONString(new APIResult(data), config, reatures);
    }
    public static String successIncludes(Object data, Class<?> clazz, String... includes) {
        SimplePropertyPreFilter filter = new SimplePropertyPreFilter(clazz, includes);
        return JSON.toJSONString(new APIResult(data), config, filter, reatures);
    }
    public static String successExcludes(Object data, Class<?> clazz, String... excludes) {
        SimplePropertyPreFilter filter = new SimplePropertyPreFilter(clazz);
        for(String exclude : excludes) {
            filter.getExcludes().add(exclude);
        }
        return JSON.toJSONString(new APIResult(data), config, filter, reatures);
    }
    public static String successMulFilter(Object data, MulPropertyPreFilter mulFilter) {
        return JSON.toJSONString(new APIResult(data), config, mulFilter, reatures);
    }

}

